package com.glory.gloryquickui.model

import com.glory.gloryquickui.base.BaseRepository
import com.glory.gloryquickui.entity.BaseRequestBean
import com.glory.gloryquickui.entity.BaseResponseBean
import com.glory.gloryquickui.entity.DictionaryResponseBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import org.json.JSONObject
import rxhttp.toClass
import rxhttp.toStr
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse
import rxhttp.wrapper.param.toResponse2

/**
 * @author HX
 * @date 2021/4/20
 * @desc 网络请求逻辑
 */
class QuickUIRepository : BaseRepository() {


    suspend fun getFormJsonById(formId: String): BaseResponseBean<Any> {
        return RxHttp.postForm(FormUrl)
            .add("id", formId)
            .toClass<BaseResponseBean<Any>>()
            .await()
    }

    suspend fun getDataByOneTable(map: BaseRequestBean): String {
        return RxHttp.postJson(BaseUrl + "queryByTableName")
            .addAll(Gson().toJson(map))
            .toResponse<String>()
            .await()
    }

    suspend fun getDataByThird(map: BaseRequestBean): String {
        return RxHttp.postJson(BaseUrl + "queryHandle")
            .addAll(Gson().toJson(map))
            .toResponse<String>()
            .await()
    }

    suspend fun getDataByMultipleTable(map: BaseRequestBean): String {
        return RxHttp.postJson(BaseUrl + "queryNoPageByMultipleTableName")
            .addAll(Gson().toJson(map))
            .toResponse<String>()
            .await()
    }

    suspend fun operateDataByOneTable(map: BaseRequestBean): BaseResponseBean<Any> {
        return RxHttp.postJson(BaseUrl + "singleTableHandle")
            .addAll(Gson().toJson(map))
            .toClass<BaseResponseBean<Any>>()
            .await()
    }

    suspend fun operateDataByMultipleTable(map: JsonObject): String {
        return RxHttp.postJson(BaseUrl + "multipleTableHandle")
            .addAll(map)
            .toStr()
            .await()
    }

    suspend fun operateDeleteHandleTable(map: BaseRequestBean): BaseResponseBean<Any>{
        return RxHttp.postJson(BaseUrl + "deleteHandle")
            .addAll(Gson().toJson(map))
            .toClass<BaseResponseBean<Any>>()
            .await()
    }

    suspend fun getDataByInterfacePost(map:Map<String,Any>,url:String): String{
        return RxHttp.postJson(url)
            .addAll(map)
            .toStr()
            .await()
    }

    suspend fun getDataByInterfaceGet(map:Map<String,Any>,url:String): String{
        return RxHttp.get(url)
            .addAll(map)
            .toStr()
            .await()
    }

    suspend fun operateDictionaryList(map:Map<String,Any>): BaseResponseBean<ArrayList<DictionaryResponseBean>>{
        return RxHttp.postJson(DictionaryUrl + "list")
            .addAll(Gson().toJson(map))
            .toClass<BaseResponseBean<ArrayList<DictionaryResponseBean>>>()
            .await()
    }

    suspend fun syncExecuteWorkflowByName(map:Map<String,Any>): String{
        return RxHttp.postJson(WorkflowUrl + "syncExecuteWorkflowByName")
            .addAll(Gson().toJson(map))
            .toResponse2<String>()
            .await()
    }
}